package com.cn.yang.servlet.aboutUs;

import com.alibaba.fastjson.JSONObject;
import com.cn.yang.model.aboutUs.AboutUsModel;
import com.cn.yang.model.contactUs.ContactUsModel;
import com.cn.yang.service.AboutUsService;
import com.cn.yang.service.ContactUsService;
import com.cn.yang.service.impl.AboutUsServiceImpl;
import com.cn.yang.service.impl.ContactUsServiceImpl;
import com.cn.yang.util.UpLoad;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/aboutUs/updatAboutUs")
public class UpdateAboutUsServlet extends HttpServlet {
    //查询数据，返回==============
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        Integer id = Integer.valueOf(req.getParameter("id"));
        AboutUsService service = new AboutUsServiceImpl();
        AboutUsModel model = service.selectById(id);
        req.getSession().setAttribute("bgimg",model.getBgimg());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aboutUsModel",model);
        resp.getWriter().println(jsonObject);
    }
    // 更新数据库===========================================================
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        UpLoad upLoad = new UpLoad();
        String string = upLoad.imgUpload(req);
        String aa[] = string.split("@@");
        String a = (String) req.getSession().getAttribute("bgimg");
        if (aa[1].toString().equals("undefined")){
            aa[1]=a;
        }
        AboutUsModel model = new AboutUsModel();
        model.setTitle(aa[0]);
        model.setBgimg(aa[1]);
        model.setContent(aa[2]);

        AboutUsService service = new AboutUsServiceImpl();
        int result = service.updateAboutUs(model);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);

    }
}
